//
//  Set.c
//  Math03
//
//  Created by Xavier on 2023/10/11.
//

#include <stdio.h>
//1.编写程序，实现去除数组中重复元素。
//提示：int arr[]={1,2,3,1,2,3,4,5};
//消去重复元素之后为1 2 3 4 5
//2.编写程序，实现集合并运算和交运算。禁止调用高级语言集合运算功能。
////请你实现集合的并运算 set1 u set2
//public static int[] unionOfSet(int[] set1, int[] set2);
////请你实现集合的交运算 set1 n set2
//public static int[] intersectionOfSet(int[] set1, int[] set2);

//除去重复元素
/*
 1、首先创建一个空数组全部设置为空，把数据传进去
 2、对传进去的数据+1，如果数值为1的话那就是出现一次，反之没出现或者出现多次
 */
int* removalSameElem(int* arr, int len, int *resLen){
    int a[100] = {0};
    int sz = sizeof(a)/sizeof(a[0]);
    for (int i = 0; i < len; i++) {
        ++a[*(arr+i)];
    }
    static int res[] = {0};
    int j = 0;
    for (int i = 0; i < sz; i++) {
        if (a[i] != 0) {
            res[j] = i;
            ++j;
        }
    }
    resLen[0] = j;
    return res;
}

int removalSameElem2(int* arr, int len){
    int count = 0;
    for (int i = 0; i < len-1; i++) {
        if (*(arr+i) == *(arr+i+1)) {
            ++count;
            for (int j = i; j < len; j++) {
                *(arr+j) = *(arr+j+1);
            }
        }
    }
    return count;
}

//并运算
int unionLen = 1;
int* unionOfSet(int* set1, int* set2){
    int* tmpSet1 = set1;
    int* tmpSet2 = set2;
    int a[100] = {0};
    //定义全局变量
    static int unionResArr[] = {0};
    
    int len1 = 0, len2 = 0;
    //求set长度
    while (*tmpSet1) {
        tmpSet1++;
        len1++;
    }
    while (*tmpSet2) {
        tmpSet2++;
        len2++;
    }
    //给a数组赋值
    for (int i = 0; i < len1; i++) {
        ++a[*(set1+i)];
    }
    for (int i = 0; i < len2; i++) {
        ++a[*(set2+i)];
    }
    //static int unionLen = 1;
    unionLen--;
    for (int i = 0; i < 100; i++) {
        if (a[i] != 0) {
            unionResArr[unionLen] = i;
            unionLen++;
        }
    }
    return unionResArr;
}

//交运算
int interSectionLen = 1;
int* intersectionOfSet(int* set1, int* set2){
    int* tmpSet1 = set1;
    int* tmpSet2 = set2;
    int a[100] = {0};
    //定义全局变量
    static int interSectionResArr[] = {0};
    int len1 = 0, len2 = 0;
    //求set长度
    while (*tmpSet1) {
        tmpSet1++;
        len1++;
    }
    while (*tmpSet2) {
        tmpSet2++;
        len2++;
    }
    //给a数组赋值
    for (int i = 0; i < len1; i++) {
        ++a[*(set1+i)];
    }
    for (int i = 0; i < len2; i++) {
        ++a[*(set2+i)];
    }
    --interSectionLen;
    for (int i = 0; i < 100; i++) {
        if (a[i] > 1) {
            interSectionResArr[interSectionLen] = i;
            interSectionLen++;
        }
    }
    return interSectionResArr;
}

//打印数组
void printArr(int* arr, int len) {
    printf("{");
    for(int i = 0; i < len; i++) {
        printf("%d", *(arr+i));
        if (i != len-1) {
            printf(",");
        }
    }
    printf("}\n");
}

void bubbleSort(int* arr, int len) {
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < len - i - 1; j++) {
            if (*(arr+j) > *(arr+j+1)) {
                int tmp = *(arr+j);
                *(arr+j) = *(arr+j+1);
                *(arr+j+1) = tmp;
            }
        }
    }
}

int main(int argc, const char * argv[]) {
    
    printf("arr1:");
    int arr1[] = {1,2,3,1,2,3,4,5};
    int len = sizeof(arr1)/sizeof(arr1[0]);
    printArr(arr1, len);
    
//way1:
//    bubbleSort(arr1, len);
//    printf("去重后：");
//    int sortLen = removalSameElem2(arr1, len);
//    printArr(arr1, sortLen);
    
//way1:
    printf("去重后：");
    int resLen[] = {0};
    int* p = removalSameElem(arr1, len, resLen);
    printArr(p, resLen[0]);

    printf("---------------------------------------------\n");

    int set1[] = {1,2,3,4,5};
    int setLen1 = sizeof(set1)/sizeof(set1[0]);
    int set2[] = {3,4,5,6,7,8,9};
    int setLen2 = sizeof(set2)/sizeof(set2[0]);
    printf("set1:");
    printArr(set1, setLen1);
    printf("set2:");
    printArr(set2, setLen2);
    
    int* unionSet = unionOfSet(set1, set2);
    printf("并集：");
    printArr(unionSet, unionLen);
    int* intersectionSet = intersectionOfSet(set1, set2);
    printf("交集：");
    printArr(intersectionSet, interSectionLen);
}
